package net.ondev.dutils.tostring;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IType;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class ToStringActionDelegate extends BaseActionDelegate implements IObjectActionDelegate {
	static Log log = LogFactory.getLog(ToStringActionDelegate.class);
	
	IWorkbenchPart part;

	List<IType> targetElements=new ArrayList<IType>();
	
	ToStringCodesGenerator generator=new ToStringCodesGenerator();
	

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		part = targetPart;
	}


	public void selectionChanged(IAction action, ISelection selection) {
		setSelectedElements(selection);
	}

	protected void setSelectedElements(ISelection selection) {
		targetElements.clear();
		
		List selectedElements = null;
		if(selection !=null && selection instanceof IStructuredSelection){
			selectedElements = ((IStructuredSelection)selection).toList();
		}
				if(selectedElements == null){
			return;
		}
		
		for(Object i: selectedElements){
			
			if(i instanceof ICompilationUnit){
				addTargetElements((ICompilationUnit)i);
			}else if(i instanceof IType){
				targetElements.add((IType)i);
			}
		}
	}

}
